import { IsInt, Min, Max, IsString, IsEnum, IsDateString, IsOptional, Type } from 'class-validator';
import { Transform } from 'class-transformer';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';

/**
 * 任务状态枚举
 */
export enum TaskStatus {
  QUEUED = 'QUEUED',
  ANALYZING = 'ANALYZING',
  COMPLETED = 'COMPLETED',
  FAILED = 'FAILED',
  CANCELLED = 'CANCELLED',
  TIMEOUT = 'TIMEOUT',
}

/**
 * 任务列表查询DTO
 * 零隐患MFIS规范 - 严格的查询参数验证
 */
export class TaskListDto {
  @ApiProperty({
    description: '页码',
    example: 1,
    minimum: 1,
    default: 1,
  })
  @IsInt({ message: '页码必须是整数' })
  @Min(1, { message: '页码必须大于0' })
  @Type(() => Number)
  page: number = 1;

  @ApiProperty({
    description: '每页数量',
    example: 10,
    minimum: 1,
    maximum: 100,
    default: 10,
  })
  @IsInt({ message: '每页数量必须是整数' })
  @Min(1, { message: '每页数量必须大于0' })
  @Max(100, { message: '每页数量不能超过100' })
  @Type(() => Number)
  page_size: number = 10;

  @ApiPropertyOptional({
    description: '患者编号筛选',
    example: 'PAT-2024-001',
  })
  @IsString({ message: '患者编号必须是字符串' })
  @IsOptional()
  @Transform(({ value }) => value?.trim())
  patient_code?: string;

  @ApiPropertyOptional({
    description: '任务状态筛选',
    enum: TaskStatus,
    example: TaskStatus.COMPLETED,
  })
  @IsEnum(TaskStatus, { message: '任务状态值无效' })
  @IsOptional()
  @Transform(({ value }) => value?.toUpperCase())
  status?: TaskStatus;

  @ApiPropertyOptional({
    description: '开始日期',
    example: '2024-01-01',
    type: 'string',
    format: 'date',
  })
  @IsDateString({}, { message: '开始日期格式无效' })
  @IsOptional()
  date_from?: string;

  @ApiPropertyOptional({
    description: '结束日期',
    example: '2024-12-31',
    type: 'string',
    format: 'date',
  })
  @IsDateString({}, { message: '结束日期格式无效' })
  @IsOptional()
  date_to?: string;
}

/**
 * 分页响应基础接口
 */
export interface PaginatedResponse<T> {
  items: T[];
  pagination: {
    current_page: number;
    page_size: number;
    total: number;
    total_pages: number;
  };
}